import { mapGetters, mapActions } from 'vuex';
import { addHeaderCssForTheme } from './common.js';
import {
	setBookObjectProperty,getBookObjectProperty
} from './localstorage.js';
export const ebookMixin = {
	computed:{
		...mapGetters([
			'fileName',
			'menuVisble',
			'settingVisible',
			'defaultFontSize',
			'defaultFontFamily',
			'defaultTheme',
			'fontFamilyVisible',
			'currentBook',
			// 'progress', // 这里的值用于element ui组件的v-model，如果在这里混入，会报错没有setter方法。解决方法是删除这里的混入，而在调用页面中的计算属性中 设置其get和set方法
			'bookAvailable',
			'section',
			'coverUrl',
			'metaData',
			'navigation',
			'offsetY',
			'isBookMark'
		]),
	},
	methods: {
		...mapActions([
			'setmenuVisble',
			'setfilename',
			'setsettingVisible',
			'setdefaultFontSize',
			'setdefaultFontFamily',
			'setdefaultTheme',
			'setfontFamilyVisible',
			'setcurrentBook',
			'setprogress',
			'setbookAvailable',
			'setsection',
			'setcoverUrl',
			'setmetaData',
			'setnavigation',
			'setoffsetY',
			'setisBookMark'
		]),
		// 初始化设置页面主题
		initPageTheme() {
			let href = `${process.env.VUE_APP_RESOURCE_URL}/theme/${this.defaultTheme}.css`
			addHeaderCssForTheme(href)
		},
		// 刷新进度条位置
		refreshLocation() {
			const  currentLocationInfo = this.currentBook.rendition.currentLocation()
			// 通过cfi去获得当前 Progress
			if(currentLocationInfo && currentLocationInfo.start && currentLocationInfo.start.cfi) {
				const startCfi = currentLocationInfo.start.cfi
				const currentProgress = this.currentBook.locations.percentageFromCfi(startCfi)
				this.setprogress({
					progress : Math.floor(currentProgress * 100)
				})
				// 存储当前位置
				setBookObjectProperty(this.fileName,'location',startCfi)
				
				// 判断当前页 是否为书签
				let bookMarkList = getBookObjectProperty(this.fileName,'bookMarkList')
				if(!bookMarkList) {
					this.setisBookMark({
						isBookMark : false
					})
				}else {
					let flag = bookMarkList.some(item => item.cfi === startCfi)
					if(flag) {
						this.setisBookMark({
							isBookMark : true
						})
					}else {
						this.setisBookMark({
							isBookMark : false
						})
					}
				}
			} 
		},
		// 初始化 section
		handleInitSection(href) {
			const  currentLocationInfo = this.currentBook.rendition.currentLocation()
			let sectionHref =  href ? href : currentLocationInfo.start.href
			
			for(let i = 0; i < this.navigation.length; i++) {
				const targetHref = this.navigation[i].href
				if(targetHref.indexOf(sectionHref) !== -1) {
					this.setsection({
						section : i
					})
					break;
				}
			}
		},
		
		/**
		 *  电子书渲染
		 * @param {Object} target
		 * @param function cb 如果有回调函数 则执行回调函数
		 */
		display(target, cb) {
			if(target) {
				this.currentBook.rendition.display(target).then(() => {
					this.refreshLocation()
					if (cb) cb()
				})
			}else {
				this.currentBook.rendition.display().then(() => {
					this.refreshLocation()
					if (cb) cb()
				})
			}
		},
		// 隐藏菜单
		hideTitleAndMenu() {
			this.setmenuVisble({
				menuVisble : false
			})
			this.setsettingVisible({
				settingVisible : -1
			})
			this.setfontFamilyVisible({
				fontFamilyVisible : false
			})
		},
	}
}